//
//  GetImageFromLocal.swift
//  WriteToLocal
//
//  Created by 郑东喜 on 2017/3/2.
//  Copyright © 2017年 郑东喜. All rights reserved.
//

import UIKit

class GetImageFromLocal: UIViewController {
    
    lazy var imgView: UIImageView = {
        let f : UIImageView = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 100))
//        f.backgroundColor = .red
        
        return f
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.addSubview(imgView)
        
        imgView.center = view.center
     
        
        let documentsDirectoryURL = try! FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        // create a name for your image
        let fileURL = documentsDirectoryURL.appendingPathComponent("Savedframe.png")
        
        
//        if !FileManager.default.fileExists(atPath: fileURL.path) {
//            do {
//                try UIImagePNGRepresentation(imgView.image!)!.write(to: fileURL)
//                print("Image Added Successfully")
//            } catch {
//                print(error)
//            }
//        } else {
//            print("Image Not Added")
//        }
//
//
//        func createFolder(name:String,baseUrl:NSURL){
//            let manager = FileManager.default
//            let folder = baseUrl.appendingPathComponent(name, isDirectory: true)
//            print("文件夹: \(folder)")
//            let exist = manager.fileExists(atPath: folder!.path)
//            if !exist {
//                try! manager.createDirectory(at: folder!, withIntermediateDirectories: true,
//                                             attributes: nil)
//            }
//        }
//        
//        //在文档目录下新建folder目录
//        let manager = FileManager.default
//        let urlForDocument = manager.urls(for: .documentDirectory, in: .userDomainMask)
//        let url = urlForDocument[0] as NSURL
//        createFolder(name: "folder", baseUrl: url)
        
        let manager = FileManager.default
        let urlForDocument = manager.urls(for: .documentDirectory, in:.userDomainMask)
        let url = urlForDocument[0] as URL
        print(url)
        
        let contentsOfPath = try? manager.contentsOfDirectory(atPath: NSHomeDirectory() + "/Documents/bj/zip")
        print("contentsOfPath: \(contentsOfPath)")
        
        
        let filePath = NSHomeDirectory() + "/Documents/bj/zip/nav_1@2x.png"
        let image = UIImage(named: filePath)
        
        imgView.image = image
        
//        let data:Data = UIImagePNGRepresentation(image!)!
//        try? data.write(to: URL(fileURLWithPath: filePath))
    }

}
